package com.ww.java8.collect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class ListAndSetDemo {

	public static void main(String[] args) {
//		test01();
		
//		List<String> list = new ArrayList<>();
//		list.add("abc");
//		System.out.println("①list的大小：" + list.size());
//		display(list);
//		test02(list);
//		System.out.println("③list的大小：" + list.size());
//		display(list);
		
//		List<Person> personList = new ArrayList<>();
//		personList.add(new Person("赵匡胤", 10));
//		System.out.println("①list的大小：" + personList.size());
//		display(personList);
//		test03(personList);
//		System.out.println("\n③list的大小：" + personList.size());
//		display(personList);
		
		Map<String, String> map = new HashMap<>();
		map.put("test01", "test01");
		System.out.println("01：" + map.size());
		displayMap(map);
		test04(map);
		System.out.println("03：" + map.size());
		displayMap(map);
	}
	
	static void displayMap(Map<String, String> map) {
		Set<Entry<String,String>> entrySet = map.entrySet();
		for (Entry<String, String> entry : entrySet) {
			System.out.println(entry.getKey() + ": " + entry.getValue());
		}
	}
	
	static <T> void display(List<T> list) {
		for (T str : list) {
			System.out.print(str + ", ");
		}
	}
	
	static void test01() {
		Set<String> set1 = new HashSet<>();
		set1.add("a");
		set1.add("b");
		set1.add("c");
		set1.add("d");
		
		
		Set<String> set2 = new HashSet<>();
		set2.add("a");
		set2.add("b");
	
		// 这是真的在原来对象上删除
		set1.removeAll(set2);
		
		set1.stream().forEach(System.out::println);
	}
	
	/**
	 * String类型
	 * @param hello
	 */
	static void test02(List<String> hello) {
//		hello.clear();
		hello.add("hello");
		hello.add("world");
		hello.add("sun");
		hello.add("hi");
		System.out.println("\n②内部list大小：" + hello.size());
	}	
	
	static void test03(List<Person> personList) {
		personList = new ArrayList<>();
		personList.add(new Person("张三", 20));
		personList.add(new Person("李四", 22));
		personList.add(new Person("王五", 23));
		personList.add(new Person("赵柳", 21));
		System.out.println("\n②内部list大小：" + personList.size());
		display(personList);
	}
	
	static void test04(Map<String, String> map) {
		map = new HashMap<>();
		map.put("k1", "v1");
		map.put("k2", "v2");
		map.put("k3", "v3");
		System.out.println("内部02：" + map.size());
		displayMap(map);
	}
}

class Person {
	private String name;
	private Integer age;
	
	public Person() {}
	
	public Person(String name, Integer age) {
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}	
	
}